﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DanhMucSanPhamProvider
/// CuongBH
/// 07/12/2011
/// </summary>
/// 
namespace EHTWebsite.Code
{
    public class DanhMucSanPhamProvider
    {

        /// <summary>
        /// ham insert du lieu vao database
        /// </summary>
        /// <param name="theDanhMucSanPham"></param>
        /// <param name="IsAdd"></param>
        /// <returns></returns>
        public static Int32 IUDanhMucSanPham(DanhMucSanPham theDanhMucSanPham, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUDanhMucSanPham";
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter theDanhMucSanPhamParameter = new SqlParameter();

            theDanhMucSanPhamParameter.ParameterName = "@DanhMucIDOut";
            theDanhMucSanPhamParameter.Direction = ParameterDirection.Output;
            theDanhMucSanPhamParameter.DbType = DbType.Int32;

            command.Parameters.Add(theDanhMucSanPhamParameter);

            command.Parameters.Add(new SqlParameter("@DanhMucID", theDanhMucSanPham.DanhMucID));
            command.Parameters.Add(new SqlParameter("@TenDanhMucVN", theDanhMucSanPham.TenDanhMucVN));
            command.Parameters.Add(new SqlParameter("@TenDanhMucOther", theDanhMucSanPham.TenDanhMucOther));
            command.Parameters.Add(new SqlParameter("@SapXep", theDanhMucSanPham.SapXep));           
            command.Parameters.Add(new SqlParameter("@MenuLevel", theDanhMucSanPham.MenuLevel));
            command.Parameters.Add(new SqlParameter("@IconTrangThai", theDanhMucSanPham.IconTrangThai));
            command.Parameters.Add(new SqlParameter("@ChuyenMucChaID", theDanhMucSanPham.ChuyenMucChaID));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);
            return Int32.Parse(command.Parameters["@DanhMucIDOut"].Value.ToString());
        }

        /// <summary>
        /// ham xoa danh muc san pham theo ID
        /// </summary>
        /// <param name="DanhMucID"></param>

        public static void DeleteDanhMucSanPhamByDanhMucID(Int32 DanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteDanhMucSanPhamByDanhMucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));

            DataProvider.ExecuteNoneQuery(command);
        }


        /// <summary>
        /// ham lay danh muc san pham 
        /// </summary>
        /// <param name="DanhMucID"></param>
        /// <param name="tenDanhMucVN"></param>
        /// <param name="tenDanhMucOther"></param>
        /// <returns></returns>
        public static DataSet GetDanhMucSanPhams(Int32 DanhMucID, String tenDanhMucVN, String tenDanhMucOther)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucSanPhams";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));
            command.Parameters.Add(new SqlParameter("@TenDanhMucVN", tenDanhMucVN));
            command.Parameters.Add(new SqlParameter("@TenDanhMucOther", tenDanhMucOther));
           
           

            return DataProvider.ExecuteDataSet(command);
        }
      

        /// <summary>
        /// ham lay danh muc san pham theo chuyen muc cha ID
        /// neu parentID = -1 thif select all
        /// </summary>
        /// <param name="DanhMucID"></param>
        /// <returns></returns>
        public static DataSet GetDanhMucSanPhamByParentID(Int32 DanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetDanhMucSanPhamsByParentID]";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));            

            return DataProvider.ExecuteDataSet(command);
        }

        /// <summary>
        /// ham lay tin tuc san pham hot
        /// </summary>
        /// <returns></returns>
        public static DataSet GetTinTuc_SanPhamHost()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_NewsAndProductHost";
            command.CommandType = CommandType.StoredProcedure;                                  

            return DataProvider.ExecuteDataSet(command);
        }

        /// <summary>
        /// ham lay danh muc san pham cho homepage
        /// </summary>
        /// <param name="DanhMucID"></param>
        /// <returns></returns>
        public static DataSet GetDanhMucSanPhamsForFrontEnd(Int32 DanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucSanPhamsForFrontEnd";
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        
        /// <summary>
        /// ham update luot truy cap cua danh muc san pham
        /// </summary>
        /// <param name="DanhMucID"></param>
        public static void UpdateLuotTruyCap(Int32 DanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UDanhMucSanPham_SoLuongTruyCap";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// ham lay danh muc san pham theo ID
        /// </summary>
        /// <param name="DanhMucID"></param>
        /// <returns></returns>
        public static DanhMucSanPham GetDanhMucSanPhamByDanhMucID(Int32 DanhMucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucSanPhamByDanhMucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            DanhMucSanPham theDanhMucSanPham = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theDanhMucSanPham = new DanhMucSanPham();               
                theDanhMucSanPham.TenDanhMucVN = ds.Tables[0].Rows[0]["TenDanhMucVN"].ToString();

                theDanhMucSanPham.IconTrangThai = ds.Tables[0].Rows[0]["IconTrangThai"].ToString();
                theDanhMucSanPham.TenDanhMucOther = ds.Tables[0].Rows[0]["TenDanhMucOther"].ToString();               
                theDanhMucSanPham.LuongTruyCap = Int32.Parse(ds.Tables[0].Rows[0]["LuongTruyCap"].ToString());
                theDanhMucSanPham.SapXep = Int32.Parse(ds.Tables[0].Rows[0]["SapXep"].ToString());
                theDanhMucSanPham.DanhMucID = Int32.Parse(ds.Tables[0].Rows[0]["DanhMucID"].ToString());
                theDanhMucSanPham.MenuLevel = Int32.Parse(ds.Tables[0].Rows[0]["MenuLevel"].ToString());
                theDanhMucSanPham.ChuyenMucChaID = Int32.Parse(ds.Tables[0].Rows[0]["ChuyenMucChaID"].ToString());
            }

            return theDanhMucSanPham;
        }

        /// <summary>
        /// ham thay doi trang thai cua danh muc san pham
        /// </summary>
        /// <param name="DanhMucID"></param>
        /// <param name="publish"></param>
        /// <param name="IconTrangThai"></param>
        public static void changePublish(Int32 DanhMucID, Boolean publish, String IconTrangThai)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_ChangePublishByDanhMucSanPhamID]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));
            command.Parameters.Add(new SqlParameter("@IconTrangThai", IconTrangThai));
            command.Parameters.Add(new SqlParameter("@Publish", publish));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// ham lay danh muc san pham moi nhat
        /// </summary>
        /// <returns></returns>
        public static DataSet GetTopCategorys()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTopCategory";
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }
    }
}